package com.yilu.driver.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.is.ui.filter.util.ScreenUtils;
import com.is.ui.widget.CustomPopWindow;
import com.is.ui.widget.IconButton;
import com.is.ui.xrecyclerview.XRecyclerView;
import com.is.ui.xrecyclerview.universaladapter.ViewHolderHelper;
import com.is.ui.xrecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.is.ui.xrecyclerview.universaladapter.recyclerview.OnItemClickListener;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseActivity;
import com.yilu.driver.entity.ApplyGraduationResponse;
import com.yilu.driver.entity.StageGraduationResponse;
import com.yilu.driver.mvp.contract.StageGraduationContract;
import com.yilu.driver.mvp.model.StageGraduationModel;
import com.yilu.driver.mvp.presenter.StageGraduationPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 阶段结业
 */
public class StageGraduationActivity extends BaseActivity implements StageGraduationContract.View, OnItemClickListener {
    @Bind(R.id.btn_title)
    IconButton btnTitle;
    @Bind(R.id.mFilterContentView)
    XRecyclerView mFilterContentView;
    private StageGraduationPresenter presenter;
    private StageGraduationModel model;
    private CustomPopWindow mCustomPopWindow;

    private CommonRecycleViewAdapter<StageGraduationResponse.DataBean> mAdapter;
    private CommonRecycleViewAdapter<ApplyGraduationResponse.DataBean> adapter;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_stage_graduation;
    }

    @Override
    protected void initViewsAndEvents() {
        //getStageGraduationList(1, 1, 1, 1);
        getApplyGraduationList(1, "", "", 1, 1);
        mAdapter = new CommonRecycleViewAdapter<StageGraduationResponse.DataBean>(mContext, R.layout.item_stage_graduation) {
            @Override
            public void convert(ViewHolderHelper helper, StageGraduationResponse.DataBean dataBean) {
                TextView txtName = helper.getView(R.id.txt_trainee_name);
                txtName.setText(dataBean.getName());
            }
        };
        mFilterContentView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommonRecycleViewAdapter<ApplyGraduationResponse.DataBean>(mContext, R.layout.item_stage_graduation) {
            @Override
            public void convert(ViewHolderHelper helper, ApplyGraduationResponse.DataBean dataBean) {
                TextView txtName = helper.getView(R.id.txt_trainee_name);
                txtName.setText(dataBean.getName());
            }
        };
        mFilterContentView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void initPresenter() {
        presenter = new StageGraduationPresenter(mContext);
        model = new StageGraduationModel();
        presenter.setVM(this, model);
    }

    @Override
    public void returnStageGraduation(StageGraduationResponse stageGraduationResponse) {
        mAdapter.replaceAll(stageGraduationResponse.getData());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnApplyGraduation(ApplyGraduationResponse applyGraduationResponse) {
        adapter.replaceAll(applyGraduationResponse.getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_title)
    public void onClick() {
        showPopMenu();
    }

    /**
     * 显示顶部选项菜单
     * 已登记、未登记
     */
    private void showPopMenu() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu_registion, null);
        //处理popWindow 显示内容
        handleLogic(contentView);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .size(ScreenUtils.getScreenWidth(mContext) / 3, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create()
                .showAsDropDown(btnTitle, 0, 10);
    }

    /**
     * 选项菜单的控制方法
     * 处理弹出显示内容、点击事件等逻辑
     *
     * @param contentView
     */
    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.menu1:
                        btnTitle.setText("已登记");
                        //  registion = true;
                        break;
                    case R.id.menu2:
                        btnTitle.setText("未登记");
                        //  registion = false;
                        break;
                }
                if (mCustomPopWindow != null) {
                    mCustomPopWindow.dissmiss();
                }
            }
        };
        contentView.findViewById(R.id.menu1).setOnClickListener(listener);
        contentView.findViewById(R.id.menu2).setOnClickListener(listener);
    }

    private void getStageGraduationList(Integer operator, Integer start, Integer length, Integer subject) {
        JSONObject json = new JSONObject();
        try {
            json.put("operator", operator);
            json.put("start", start);
            json.put("length", length);
            json.put("subject", subject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getStageGraduation(json);
    }

    private void getApplyGraduationList(Integer operator, String name, String idNumber, Integer start, Integer length) {
        JSONObject json = new JSONObject();
        try {
            json.put("operator", operator);
            json.put("name", name);
            json.put("idNumber", idNumber);
            json.put("start", start);
            json.put("length", length);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getApplyGraduation(json);
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, Object o, int position) {
      
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
        return false;
    }
}
